import { timeBuyGoodsClass,timeBuyIndex,timeBuyMyRemind,timeBuyDelRemind,timeBuyRemind,timeBuyIdClassGoods, } from './api';
import { Toast } from 'antd-mobile';
import {orderListApi} from "../orderList/api";
import {loginAPi} from "../login/api";
import {favgoods} from "../cart/api";
import {getIndexListAllApi} from "../home/api";
import {hotsListApi} from "../findOthers/api";

export default {
    namespace: 'timeBuy',
    state: {
        timeBuyList:[],
        timeBuyDetail:[],
        hasMore:true,
        goodsClassEntityFlashList:[],
        timeBuyIdClassGoodsList:[],
        ret:[],
        pageNo:1,
        timeBuyMyRemindList: [],
        flashSaleRemindNewEntity:[],
        flashSaleRemindNotBeginList:[],
    },
    reducers: {
        save(state, { data }) {
            return { ...state, ...data };
        },
        clearState() {
            return {timeBuyList: [], goodsClassEntityFlashList:[],timeBuyIdClassGoodsList: [],timeBuyMyRemindList: []};
        }
    },
    effects: {
        *timeBuyListEFF({ obj }, { put, call ,select}) {
            Toast.loading('加载中...', 3);
            const result = yield call(timeBuyIndex, obj);
            console.log(result)
            if ( result.code == 200 ) {
                if( result.ret !='' ){
                    yield put({ type: 'save', data: {timeBuyList: result.ret.list || [],pageSize:result.ret.pageSize,nowTime:result.ret.nowTime} });
                    Toast.hide();
                }else {
                    yield put({ type: 'save', data: { hasMore: false }});
                }
                return { r:true };
            }
            return result
        },
        *timeBuyGoodsClassEFF({ }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(timeBuyGoodsClass);
            if (result.code == 200) {
                yield put({ type: 'save',data:{goodsClassEntityFlashList: result.ret.list||[],gcId:result.ret.list[0].id }});
                Toast.hide();
            } else {

            }
        },
        *timeBuyIdClassGoodsEFF({ obj }, { put, call, select }) {
            const timeBuy = yield select((state)=>state.timeBuy);
            const result = yield call(timeBuyIdClassGoods, { gcId:timeBuy.gcId, pageNo:timeBuy.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { timeBuyIdClassGoodsList: []} })}else{
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, timeBuyIdClassGoodsList: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,timeBuyIdClassGoodsList: [...timeBuy.timeBuyIdClassGoodsList, ...result.ret.list]} });

                    return { r:true };
                }
            }
            return result
        },
        *timeBuyMyRemindEFF({ obj }, { put, call, select }) {
            const timeBuy = yield select((state)=>state.timeBuy);
            const result = yield call(timeBuyMyRemind, {  pageNo:timeBuy.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { timeBuyMyRemindList: []} })}else{
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, timeBuyMyRemindList: result.ret.list} });

                    else yield put({ type: 'save', data: { ...result.ret,timeBuyMyRemindList: [...timeBuy.timeBuyMyRemindList, ...result.ret.list]} });

                    return { r:true };
                }
            }
            return result
        },
        *timeBuyRemindEFF({ obj }, { put, call }) {
            Toast.loading();
            const result = yield call(timeBuyRemind,obj);
            return result;
        },
        *timeBuyDelRemindEFF({ obj }, { put, call }) {
            Toast.loading();
            const result = yield call(timeBuyDelRemind,obj);
            return result;
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                // console.log(pathname)
                if(pathname.indexOf("/timeBuy")==-1||pathname == '/timeBuyClass'||pathname == '/timeBuyRemind'){
                    dispatch({type:"clearState"})
                }
            });
        },
    },
};
